import Storage from '@/utils/Storage'

const setUser = function (user) {
  return Storage.set('auth_user', user)
}

const getUser = function () {
  return Storage.get('auth_user', null)
}

const setAccessToken = function (token) {
  return Storage.set('auth_token', token)
}

const getAccessToken = function () {
  const token = Storage.get('auth_token', '')

  try {
    if (JSON.parse(atob(token.split('.')[1])).exp * 1000 > (new Date()).getTime()) {
      return token
    }
  } catch (e) {
  }

  setAccessToken('')
  return ''
}

const isLogin = function () {
  return !!getAccessToken()
}

// const isLogin = function () {
//   return !!getUser()
// }

const logout = function () {
  setUser(null)
  setAccessToken('')
}

export default {
  setUser,
  getUser,
  setAccessToken,
  getAccessToken,
  isLogin,
  logout
}
